package demo;

import org.apache.commons.io.IOUtils;
import org.csource.fastdfs.*;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.FileOutputStream;


public class Demo000001 {
    /**
     * 上传
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        ClassPathResource path=new ClassPathResource("client.properties");
        //初始化配置文件
        ClientGlobal.init(path.getClassLoader().getResource("client.properties").getPath());
        Long starttime=System.currentTimeMillis();
        //创建Tracker客户对象
        TrackerClient tc = new TrackerClient();
        //创建Tracker服务器对象
        TrackerServer ts = tc.getConnection();
        //创建Storage服务器
        StorageServer ss = null;
        //创建Storage客户端
        StorageClient1 sc = new StorageClient1(ts, ss);
        //实现上传
        // String[] pngs = sc.upload_file("F:/ix/1.png", "png", null);
        // String[] pngs = sc.upload_file("D:/Nwt/recv/戴珍珠耳环的少女/一拳人 第二季 第09集 最強的煩惱 720P AVC.mp4", "mp4", null);
        // String[] pngs = sc.upload_file("D:/Nwt/recv/白哥牛逼/BeanSub.mp4", "mp4", null);
         String[] pngs = sc.upload_file("D:/Nwt/recv/白/demo.mp4", "mp4", null);
        Long endtime=System.currentTimeMillis();
        for (String s : pngs) {
            System.out.println(s);
        }


        System.out.println(endtime-starttime);
    }

    /**
     * 下载
     * @throws Exception
     */
    @Test
    public void testDownload() throws Exception {
        ClassPathResource path=new ClassPathResource("client.properties");
        //初始化配置文件
        ClientGlobal.init(path.getClassLoader().getResource("client.properties").getPath());
        Long starttime=System.currentTimeMillis();
        //创建跟踪器客户端对象
        TrackerClient tc = new TrackerClient();
        //获取跟踪器连接
        TrackerServer ts = tc.getConnection();
        //创建Storage服务器
        StorageServer ss = null;
        //获取存储器客户端对象
        StorageClient1 sc = new StorageClient1(ts, ss);
        //根据文件标识下载文件
        byte[] bytes = sc.download_file1("group1/M00/00/00/wKiOgl0DWuaAWcUHDwB-cGXI4o8943.mp4");
        //将数据写入输出流
        IOUtils.write(bytes, new FileOutputStream("f:/白.mp4"));
        Long endtime=System.currentTimeMillis();
        System.out.println(endtime-starttime);
    }


}
